package halma;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;


public class HalmaController implements ActionListener{
	static private HalmaController singleton;
	
	Player player[] = {};
	Player currentPlayer = new Player();
	Player nextPlayer = new Player();
	
	private HalmaMove move;
	private HalmaBoard board;
	private HalmaView view;
	
	static public HalmaController getInstance(HalmaBoard board, HalmaView view) {
	 if (singleton == null)
		singleton = new HalmaController(board, view);
	 return singleton;
	}
	
	public HalmaController(HalmaBoard board, HalmaView view) {
		this.view = view;
		this.board = board;
	}
	
	public void actionPerformed(ActionEvent e) {
		
		if(view instanceof HalmaView) {
		} else {
			System.out.println("view ist nicht instanziert");
		}
		
		//button has been pressed
		JButton src =(JButton) e.getSource();
		Rectangle rect = src.getBounds();
		int yPos = rect.y/HalmaSettings.BUTTON_SIZE_HEIGHT;
		int xPos = rect.x/HalmaSettings.BUTTON_SIZE_WIDTH;
		int zeile = yPos -2;
		int spalte = xPos - 7;
		
		src.setIcon(new ImageIcon("img/yellow.png"));
		
		if (move instanceof HalmaMove) {
			//set to-coordinates
			move.setToXPosition(zeile);
			move.setToYPosition(spalte);
			
			//if valid execute move
			if (board.isMoveValid(move, currentPlayer)) {
				board.move(currentPlayer, move);
				if (isGameOver()) {
					//TODO view benachrichtigen, dass currentPlayer gewonnen hat
					
					System.out.println(currentPlayer.getId() + " hat gewonnen!");
				}
				currentPlayer = getNextPlayer(player);
				view.setMoveStatus(0);
			} else {
				view.setMoveStatus(-1);
			}
			//reset
			move = null;
		} 
		else {
			//set from-coordinates
			move = new HalmaMove();
			move.setFromXPosition(zeile);
			move.setFromYPosition(spalte);
			view.setMoveStatus(1);
		}
		view.displayStatus(currentPlayer);
	}
	
	
	protected Player getNextPlayer(Player players[]) {
		for (int i = 0; i < players.length; i++) {	
			if(players[i] == currentPlayer) {
				if(i+1 == players.length) {
					nextPlayer = players[0];
				} 
				else {
					nextPlayer = players[i+1];
				}
			}
		}
		return nextPlayer;		
	}
	
	protected boolean isGameOver() {
		//count figures
		int inEndBase = 0;
		for (int i = 0; i < board.getHeight(); i++) {
			for (int j = 0; j < board.getWidth(); j++) {
				if (currentPlayer.getId() + 1 == board.getField(i, j).getEndBaseFromPlayer()
				  && board.getField(i, j).getEndBaseFromPlayer() == board.getField(i, j).getFieldType())
					inEndBase++;
			}
		}
		if (inEndBase == 10)
			return (true);
		return (false);
	}
	
	protected void setupPlayers() {
		player = new Player[2];
		for (int i = 0; i < 2; i++) {
			player[i] = new Player(i);
			player[i].setName("S" + i);
		}
		
		//TODO read number of players using view 
		/*
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		do {
			if (HalmaSettings.NUMBER_OF_PLAYERS == 5 || HalmaSettings.NUMBER_OF_PLAYERS <= 1 || HalmaSettings.NUMBER_OF_PLAYERS > 6) {
				System.out.println("\nUngueltige Eingabe, bitte neu eingeben!");
			}

			System.out.print("Anzahl Spieler (2,3,4 oder 6): ");

			try {
				HalmaSettings.NUMBER_OF_PLAYERS = Integer.parseInt(br.readLine());
			} catch (Exception e) {
				HalmaSettings.NUMBER_OF_PLAYERS = 0;
			}
		} while (HalmaSettings.NUMBER_OF_PLAYERS == 5 || HalmaSettings.NUMBER_OF_PLAYERS <= 1 || HalmaSettings.NUMBER_OF_PLAYERS > 6);
		*/
		
		//TODO read names using view
		// Startspieler setzen durch Zufallsgenerator
		currentPlayer = player[0]; //player[(int) ((HalmaSettings.NUMBER_OF_PLAYERS) * Math.random())];
		//System.out.println("\n-----" + getNextPlayer(player).getName() + "("+ (getNextPlayer(player).getId() + 1) + ") beginnt!-----");
	}
	
}
